| Conditions | 1 |
| Paths | 1 |
| Total Lines | 166 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | |||
| 4 | describe('little endiand and big endian reading', function() { |
||
| 5 | |||
| 6 | let byteData = require('../../index.js'); |
||
| 7 | |||
| 8 | // 16-bit |
||
| 9 | it('should turn 2 16-bit signed ints to 2 bytes BE (0s)', function() { |
||
| 10 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0], |
||
| 11 | 16, {"signed": true, "be": true}), |
||
| 12 | [0, 0]); |
||
| 13 | }); |
||
| 14 | it('should turn 2 16-bit signed ints to 2 bytes LE (1s)', function() { |
||
| 15 | assert.deepEqual(byteData.fromBytes([1, 0, 1, 0], |
||
| 16 | 16, {"signed": true, "be": false}), |
||
| 17 | [1, 1]); |
||
| 18 | }); |
||
| 19 | it('should turn 2 16-bit signed ints to 2 bytes BE (1s)', function() { |
||
| 20 | assert.deepEqual(byteData.fromBytes([0, 1, 0, 1], |
||
| 21 | 16, {"signed": true, "be": true}), |
||
| 22 | [1, 1]); |
||
| 23 | }); |
||
| 24 | it('should turn 1 16-bit unsigned ints to 2 bytes BE (1s)', function() { |
||
| 25 | assert.deepEqual(byteData.fromBytes([0, 1], |
||
| 26 | 16, {"be": true}), |
||
| 27 | [1]); |
||
| 28 | }); |
||
| 29 | it('should turn 2 bytes hex to 1 16-bit float BE (1/3)', function() { |
||
| 30 | assert.deepEqual(byteData.fromBytes( |
||
| 31 | ["55", "35"], 16, {"base": 16, "float": true, "be": true})[0].toFixed(5), |
||
| 32 | 0.33325); |
||
| 33 | }); |
||
| 34 | |||
| 35 | // 24-bit |
||
| 36 | it('should turn 2 24-bit signed ints to 6 bytes BE (0s)', function() { |
||
| 37 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 0, 0], |
||
| 38 | 24, {"signed": true, "be": true}), |
||
| 39 | [0, 0]); |
||
| 40 | }); |
||
| 41 | it('should turn 2 24-bit signed ints to 6 bytes LE (1s)', function() { |
||
| 42 | assert.deepEqual(byteData.fromBytes([1, 0, 0, 1, 0, 0], |
||
| 43 | 24, {"signed": true, "be": false}), |
||
| 44 | [1, 1]); |
||
| 45 | }); |
||
| 46 | it('should turn 2 24-bit signed ints to 6 bytes BE (1s)', function() { |
||
| 47 | assert.deepEqual(byteData.fromBytes([0, 0, 1, 0, 0, 1], |
||
| 48 | 24, {"signed": true, "be": true}), |
||
| 49 | [1, 1]); |
||
| 50 | }); |
||
| 51 | it('should turn 1 24-bit unsigned int to 3 bytes BE (8388607)', function() { |
||
| 52 | assert.deepEqual(byteData.fromBytes(["7f", "ff", "ff"], |
||
| 53 | 24, {"base": 16, "signed": false, "be": true}), |
||
| 54 | [8388607]); |
||
| 55 | }); |
||
| 56 | it('should turn 2 24-bit signed ints to 6 bytes BE (max range)', function() { |
||
| 57 | assert.deepEqual(byteData.fromBytes(["80","00","00", "7f", "ff", "ff"], |
||
| 58 | 24, {"base": 16, "signed": true, "be": true}), |
||
| 59 | [-8388608, 8388607]); |
||
| 60 | }); |
||
| 61 | it('should turn 2 24-bit signed ints to 6 bytes BE', function() { |
||
| 62 | assert.deepEqual(byteData.fromBytes(["80","00","00" , "00","00","01", "7f", "ff", "ff"], |
||
| 63 | 24, {"base": 16, "signed": true, "be": true}), |
||
| 64 | [-8388608, 1, 8388607]); |
||
| 65 | }); |
||
| 66 | |||
| 67 | // 32-bit |
||
| 68 | it('should turn 2 32-bit signed ints to 8 bytes BE (0s)', function() { |
||
| 69 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 0, 0, 0, 0], |
||
| 70 | 32, {"signed": true, "be": true}), |
||
| 71 | [0, 0]); |
||
| 72 | }); |
||
| 73 | it('should turn 2 32-bit signed ints to 8 bytes LE (1s)', function() { |
||
| 74 | assert.deepEqual(byteData.fromBytes([1, 0, 0, 0, 1, 0, 0, 0], |
||
| 75 | 32, {"signed": true, "be": false}), |
||
| 76 | [1, 1]); |
||
| 77 | }); |
||
| 78 | it('should turn 2 32-bit signed ints to 8 bytes BE (1s)', function() { |
||
| 79 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 1, 0,0, 0, 1], |
||
| 80 | 32, {"signed": true, "be": true}), |
||
| 81 | [1, 1]); |
||
| 82 | }); |
||
| 83 | it('should turn 1 32-bit signed ints to 4 bytes BE (1s)', function() { |
||
| 84 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 1], |
||
| 85 | 32, {"signed": true, "be": true}), |
||
| 86 | [1]); |
||
| 87 | }); |
||
| 88 | it('should turn 8 bytes hex to 2 32-bit ints (max range)', function() { |
||
| 89 | assert.deepEqual(byteData.fromBytes( |
||
| 90 | ["80","0","0","0", "7f","ff","ff","ff"], |
||
| 91 | 32, {"base": 16, "signed": true, "be": true}), |
||
| 92 | [-2147483648,2147483647]); |
||
| 93 | }); |
||
| 94 | it('should turn 1 32-bit float from 4 bytes BE hex (2.1474836)', function() { |
||
| 95 | assert.deepEqual(byteData.fromBytes( |
||
| 96 | ["40","9","70","5f"], |
||
| 97 | 32, {"base": 16, "float": true, "be": true})[0].toFixed(7), |
||
| 98 | 2.1474836); |
||
| 99 | }); |
||
| 100 | |||
| 101 | // 40-bit |
||
| 102 | it('should turn 2 40-bit signed ints to 10 bytes BE (0s)', function() { |
||
| 103 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
||
| 104 | 40, {"signed": true, "be": true}), |
||
| 105 | [0, 0]); |
||
| 106 | }); |
||
| 107 | it('should turn 2 40-bit signed ints to 10 bytes LE (1s)', function() { |
||
| 108 | assert.deepEqual(byteData.fromBytes([1, 0, 0, 0, 0, 1, 0, 0, 0, 0], |
||
| 109 | 40, {"signed": true, "be": false}), |
||
| 110 | [1, 1]); |
||
| 111 | }); |
||
| 112 | it('should turn 2 40-bit signed ints to 10 bytes BE (1s)', function() { |
||
| 113 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 1, 0, 0, 0, 0, 1], |
||
| 114 | 40, {"signed": true, "be": true}), |
||
| 115 | [1, 1]); |
||
| 116 | }); |
||
| 117 | it('should turn 1 40-bit signed ints to 10 bytes LE (1s)', function() { |
||
| 118 | assert.deepEqual(byteData.fromBytes( |
||
| 119 | [1, 0, 0, 0, 0], 40, {"base": 10, "signed": true, "be": false}), |
||
| 120 | [1]); |
||
| 121 | }); |
||
| 122 | it('should turn 5 bytes (hex) to 1 unsigned 40-bit int (149515627075)', |
||
| 123 | function() { |
||
| 124 | assert.deepEqual(byteData.fromBytes( |
||
| 125 | ["22","cf","d3","6a","43"], 40, {"base": 16, "signed": false, "be": true}), |
||
| 126 | [149515627075]); |
||
| 127 | }); |
||
| 128 | |||
| 129 | // 48-bit |
||
| 130 | it('should turn 2 48-bit ints to 12 bytes BE (0s)', function() { |
||
| 131 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
||
| 132 | 48, {"signed": true, "be": true}), |
||
| 133 | [0, 0]); |
||
| 134 | }); |
||
| 135 | it('should turn 2 48-bit ints to 12 bytes LE (1s)', function() { |
||
| 136 | assert.deepEqual(byteData.fromBytes([1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
||
| 137 | 48, {"signed": true, "be": false}), |
||
| 138 | [1, 1]); |
||
| 139 | }); |
||
| 140 | it('should turn 2 48-bit ints to 12 bytes BE (1s)', function() { |
||
| 141 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], |
||
| 142 | 48, {"signed": true, "be": true}), |
||
| 143 | [1, 1]); |
||
| 144 | }); |
||
| 145 | it('should turn 2 48-bit unsigned ints to 12 bytes BE (1s)', function() { |
||
| 146 | assert.deepEqual(byteData.fromBytes([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], |
||
| 147 | 48, {"be": true}), |
||
| 148 | [1, 1]); |
||
| 149 | }); |
||
| 150 | it('should turn 1 48-bit ints to 6 bytes hex BE (120637438355317)', function() { |
||
| 151 | assert.deepEqual(byteData.fromBytes(["6d", "b8", "17", "a8", "e7", "75"], |
||
| 152 | 48, {"base": 16, "be": true}), |
||
| 153 | [120637438355317]); |
||
| 154 | }); |
||
| 155 | it('should turn 1 48-bit unsigned ints to 6 bytes hex BE (120637438355317)', function() { |
||
| 156 | let bytes = ["6d", "b8", "17", "a8", "e7", "75", |
||
| 157 | "00", "00", "00", "00", "00", "01", |
||
| 158 | "00", "00", "00", "00", "00", "01"]; |
||
| 159 | assert.deepEqual(byteData.fromBytes(bytes, 48, {"base": 16, "be": true}), |
||
| 160 | [120637438355317, 1, 1]); |
||
| 161 | }); |
||
| 162 | |||
| 163 | // 64-bit |
||
| 164 | it('should turn 1 64-bit float to 8 bytes BE (pi)', function() { |
||
| 165 | assert.deepEqual(byteData.fromBytes([64, 9, 33, 251, 84, 68, 45, 24], |
||
| 166 | 64, {"be": true}), |
||
| 167 | [3.141592653589793]); |
||
| 168 | }); |
||
| 169 | }); |
||
| 170 |